<template>
  <div class="scifi-stall-container" v-if="isCommonUser">
    <!-- 动态背景元素 -->
    <div class="scifi-bg-elements">
      <div class="floating-orb orb-1"></div>
      <div class="floating-orb orb-2"></div>
      <div class="floating-orb orb-3"></div>
      <div class="grid-lines"></div>
    </div>

    <div class="scifi-content-wrapper">
      <div class="scifi-card">
        <!-- 标题区域 -->
        <div class="scifi-header">
          <div class="logo-container">
            <div class="hologram-logo">
              <div class="logo-core"></div>
              <div class="logo-ring"></div>
            </div>
          </div>
          <h1 class="scifi-title">最新公告</h1>
          <p class="scifi-subtitle">智能停车管理系统</p>
        </div>

        <!-- 公告列表区域 -->
        <div class="scifi-table-panel">
          <div class="panel-header">
            <i class="iconfont icon-notice-menu"></i>
            <span>公告列表</span>
          </div>

          <div class="scifi-table-container">
            <div class="table-header">
              <div class="header-item">序号</div>
              <div class="header-item">标题</div>
              <div class="header-item">内容</div>
              <div class="header-item">操作人</div>
              <div class="header-item">创建时间</div>
            </div>

            <div v-if="notices.length === 0" class="no-data">
              <div class="no-data-text">暂无公告</div>
            </div>

            <div v-else class="table-body">
              <div v-for="(notice, index) in notices" :key="notice.id" class="table-row" @click="showNoticeDetail(notice)">
                <div class="table-cell">
                  <span class="index">{{ index + 1 }}</span>
                </div>
                <div class="table-cell">
                  <span class="title">{{ notice.title }}</span>
                </div>
                <div class="table-cell">
                  <span class="content" v-html="notice.content"></span>
                </div>
                <div class="table-cell">
                  <span class="operator">{{ notice.operator }}</span>
                </div>
                <div class="table-cell">
                  <span class="create-time">{{ dateFormat(notice.createTime, 3) }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <el-dialog
        title="公告详情"
        :visible.sync="dialogVisible"
        width="50%"
        center
    >
      <el-card>
        <div slot="header" class="notice-detail-header">
          <h2>{{ currentNotice.title }}</h2>
        </div>
        <div class="notice-detail-content">
          <div v-html="currentNotice.content"></div>
        </div>
        <div class="notice-detail-footer">
          <p>创建人：{{ currentNotice.operator }}</p>
          <p>创建时间：{{ dateFormat(currentNotice.createTime, 3) }}</p>
        </div>
      </el-card>

      <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">关 闭</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      notices: [],
      dialogVisible: false,
      currentNotice: {},
      isCommonUser: true // 默认认为是普通用户
    };
  },
  methods: {
    getList() {
      // 只有普通用户才获取公告列表
      if (!this.isCommonUser) {
        return;
      }

      axios.get("/api/notice/getLastThree").then((res) => {
        this.notices = res.data.data;
      });
    },
    showNoticeDetail(notice) {
      // 只有普通用户才能查看详情
      if (!this.isCommonUser) {
        return;
      }

      this.currentNotice = notice;
      this.dialogVisible = true;
    },
    //时间转换函数
    dateFormat(val, timeType) {
      //将字符串或长整型转为时间类型
      let date = new Date(val);
      //默认格式为'年月日 时:分:秒'
      let resultFormat = "Y年M月D日 h:m:s";
      switch (timeType) {
        case 1:
          resultFormat = "Y年M月D日";
          break;
        case 2:
          resultFormat = "Y-M-D";
          break;
        case 3:
          resultFormat = "Y-M-D h:m:s";
          break;
      }
      let result = formatTime(resultFormat);

      // 格式化日期，如月、日、时、分、秒保证为2位数
      function formatNumber(n) {
        n = n.toString();
        return n[1] ? n : "0" + n;
      }
      // 参数number为毫秒时间戳，format为需要转换成的日期格式
      function formatTime(format) {
        let newArr = [];
        let formatArr = ["Y", "M", "D", "h", "m", "s"];
        newArr.push(date.getFullYear());
        newArr.push(formatNumber(date.getMonth() + 1));
        newArr.push(formatNumber(date.getDate()));

        newArr.push(formatNumber(date.getHours()));
        newArr.push(formatNumber(date.getMinutes()));
        newArr.push(formatNumber(date.getSeconds()));

        for (let i in newArr) {
          format = format.replace(formatArr[i], newArr[i]);
        }
        return format;
      }

      return result;
    },
    // 获取最新公告
    getLastNotice() {
      // 只有普通用户才弹出公告
      if (!this.isCommonUser) {
        return;
      }

      axios.get("/api/notice/getLast").then((res) => {
        if (res.data.data) {
          const notice = res.data.data;
          this.$alert(
              `<div>
                            <p><strong>公告内容：</strong></p>
                            <p>${notice.content}</p>
                            <br/>
                            <p><strong>创建人：</strong>${notice.operator}</p>
                            <p><strong>创建时间：</strong>${this.dateFormat(notice.createTime, 3)}</p>
                        </div>`,
              notice.title,
              {
                dangerouslyUseHTMLString: true,
                confirmButtonText: '确定',
                type: 'info',
                customClass: 'notice-alert-dialog'
              }
          );
        }
      });
    },
    // 检查是否为普通用户
    checkUserType() {
      try {
        const userInfoStr = window.sessionStorage.getItem("userInfo");
        if (userInfoStr) {
          const userInfo = JSON.parse(userInfoStr);
          // 管理员用户名为admin时不显示公告
          this.isCommonUser = userInfo.username !== 'admin';
        }
      } catch (e) {
        console.error("解析用户信息失败", e);
        // 出错时默认认为是普通用户
        this.isCommonUser = true;
      }
    }
  },
  mounted() {
    this.checkUserType();
    if (this.isCommonUser) {
      this.getList();
      // 获取最新公告并在登录后弹出
      this.getLastNotice();
    }
  },
};
</script>

<style scoped lang="less">
.scifi-stall-container {
  position: relative;
  min-height: 100vh;
  background: linear-gradient(135deg, #0c0c2e 0%, #1a1a3e 50%, #2d2d5e 100%);
  overflow-x: hidden;
  font-family: 'Arial', sans-serif;
  color: #fff;
}

.scifi-content-wrapper {
  position: relative;
  z-index: 1;
  padding: 20px;
  display: flex;
  justify-content: center;
}

.scifi-card {
  position: relative;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  padding: 30px;
  width: 95%;
  max-width: 1200px;
  box-shadow:
      0 8px 32px rgba(0, 0, 0, 0.3),
      inset 0 1px 0 rgba(255, 255, 255, 0.2),
      0 0 20px rgba(0, 255, 255, 0.1);
  animation: glow 2s ease-in-out infinite alternate;
}

.scifi-header {
  text-align: center;
  margin-bottom: 30px;
}

.scifi-title {
  color: #00ffff;
  font-size: 28px;
  font-weight: 300;
  margin-bottom: 8px;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
  letter-spacing: 2px;
}

.scifi-subtitle {
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
  margin: 0;
}

.scifi-table-panel {
  margin-bottom: 20px;
}

.panel-header {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #00ffff;
  font-size: 20px;
  margin-bottom: 20px;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.3);
}

.scifi-table-container {
  background: rgba(255, 255, 255, 0.02);
  border-radius: 15px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

.table-header {
  display: grid;
  grid-template-columns: 60px 1fr 2fr 1fr 1fr;
  gap: 1px;
  background: rgba(0, 255, 255, 0.2);
  padding: 0;
}

.header-item {
  padding: 15px 10px;
  background: rgba(0, 255, 255, 0.1);
  color: #00ffff;
  font-weight: 500;
  text-align: center;
  font-size: 14px;
}

.table-body {
  max-height: 500px;
  overflow-y: auto;
}

.table-row {
  display: grid;
  grid-template-columns: 60px 1fr 2fr 1fr 1fr;
  gap: 1px;
  background: rgba(255, 255, 255, 0.02);
  transition: all 0.3s ease;
  cursor: pointer;

  &:hover {
    background: rgba(255, 255, 255, 0.05);
    transform: translateX(5px);
  }

  &:nth-child(even) {
    background: rgba(255, 255, 255, 0.03);
  }
}

.table-cell {
  padding: 12px 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
}

.index {
  color: rgba(255, 255, 255, 0.7);
  font-weight: 500;
}

.title {
  color: #00ffff;
  font-weight: 500;
}

.content {
  color: rgba(255, 255, 255, 0.9);
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.operator, .create-time {
  color: rgba(255, 255, 255, 0.9);
}

.no-data {
  padding: 60px 20px;
  text-align: center;
  background: rgba(255, 255, 255, 0.02);
}

.no-data-text {
  color: rgba(255, 255, 255, 0.5);
  font-size: 18px;
}

.notice-detail-header {
  text-align: center;
  margin-bottom: 20px;
  color: #00ffff;
}

.notice-detail-content {
  min-height: 100px;
  margin-bottom: 20px;
  color: rgba(255, 255, 255, 0.9);
}

.notice-detail-footer {
  text-align: right;
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
}

/* 背景元素样式 */
.scifi-bg-elements {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.floating-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(40px);
  opacity: 0.1;
  animation: float 6s ease-in-out infinite;
}

.orb-1 {
  width: 200px;
  height: 200px;
  background: #00ffff;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.orb-2 {
  width: 150px;
  height: 150px;
  background: #ff00ff;
  top: 60%;
  right: 10%;
  animation-delay: 2s;
}

.orb-3 {
  width: 100px;
  height: 100px;
  background: #ffff00;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

.grid-lines {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image:
      linear-gradient(rgba(0, 255, 255, 0.1) 1px, transparent 1px),
      linear-gradient(90deg, rgba(0, 255, 255, 0.1) 1px, transparent 1px);
  background-size: 50px 50px;
  opacity: 0.3;
}

/* 动画 */
@keyframes glow {
  from {
    box-shadow:
        0 8px 32px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.2),
        0 0 20px rgba(0, 255, 255, 0.1);
  }
  to {
    box-shadow:
        0 8px 32px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.2),
        0 0 30px rgba(0, 255, 255, 0.3);
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) translateX(0);
  }
  50% {
    transform: translateY(-20px) translateX(10px);
  }
}

/* 滚动条样式 */
.table-body::-webkit-scrollbar {
  width: 6px;
}

.table-body::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.05);
}

.table-body::-webkit-scrollbar-thumb {
  background: rgba(0, 255, 255, 0.3);
  border-radius: 3px;
}

.table-body::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 255, 255, 0.5);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .table-header,
  .table-row {
    grid-template-columns: 50px 1fr 2fr 1fr 1fr;
  }

  .header-item,
  .table-cell {
    font-size: 12px;
    padding: 10px 5px;
  }
}

@media (max-width: 768px) {
  .scifi-card {
    width: 95%;
    padding: 20px 15px;
  }

  .table-header,
  .table-row {
    grid-template-columns: 1fr;
    display: block;
  }

  .header-item {
    display: none;
  }

  .table-row {
    margin-bottom: 10px;
    padding: 15px;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 10px;
    border: 1px solid rgba(255, 255, 255, 0.1);

    &:hover {
      transform: translateY(-2px);
    }
  }

  .table-cell {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);

    &:last-child {
      border-bottom: none;
      justify-content: center;
      padding-top: 15px;
    }

    &::before {
      content: attr(data-label);
      color: #00ffff;
      font-weight: 500;
      font-size: 12px;
    }
  }

  .table-cell:nth-child(1)::before { content: "序号"; }
  .table-cell:nth-child(2)::before { content: "标题"; }
  .table-cell:nth-child(3)::before { content: "内容"; }
  .table-cell:nth-child(4)::before { content: "操作人"; }
  .table-cell:nth-child(5)::before { content: "创建时间"; }
}
</style>
<style>
.notice-alert-dialog {
  width: 500px;
}
</style>